//
//  ViewController.m
//  QTLifeCircle
//
//  Created by 徐腾 on 2017/1/15.
//  Copyright © 2017年 徐腾. All rights reserved.
//

#import "ViewController.h"
#import <Masonry.h>
#import "QTFeedbackViewController.h"
#import "QTPublicNavigationVC.h"

//tabBar的自控制器的个数
#define TabBarViewControllers 5
#define MoreImageViewHeight 44

//按钮的枚举
typedef NS_ENUM(NSInteger, MoreImageViewButtonType) {
    
    MoreImageViewButtonTypeYao = 10, //摇一摇按钮
    MoreImageViewButtonTypeContact,  //联系商家按钮
    MoreImageViewButtonTypeZhibo, // 直播按钮
    MoreImageViewButtonTypeWe, // 我们按钮
    MoreImageViewButtonTypeBack // 返回按钮
};

//类中类
@interface QTButton : UIButton

@end

@implementation QTButton
//取消高亮的形式
- (void)setHighlighted:(BOOL)highlighted {
    
}

@end

@interface ViewController ()
//选中的按钮
@property (weak,nonatomic) QTButton *selectedButton;

/** 更多控制器的view */
@property (weak, nonatomic) UIImageView *moreImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //自定义tabBar
    [self customTabBar];
    
    //加载更多控制器的view
    [self addMoreView];
    
    //先让更多控制器的view隐藏
    self.moreImageView.hidden = YES;
}

/**
 自定义tabBar
 */
- (void)customTabBar {
    
    //创建UIView
    UIView *tabbarView = [[UIView alloc] init];
    
    tabbarView.frame = self.tabBar.frame;
    
    tabbarView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tabbarView];
    //将tabbar隐藏
    self.tabBar.hidden = YES;
    
    //按钮的宽度
    CGFloat btnW = [UIScreen mainScreen].bounds.size.width / TabBarViewControllers;
    
    //创建5个按钮
    for (int i = 0; i < TabBarViewControllers; i++) {
        QTButton *btn = [[QTButton alloc] initWithFrame:CGRectMake(i * btnW, 0, btnW, self.tabBar.frame.size.height)];
        //设置button背景图片,不然图片不会填充按钮
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"home_%d",i]] forState:UIControlStateNormal];
        //设置button的选中状态的图片
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"home_%d_pressed",i]] forState:UIControlStateSelected];
        
        //设置按钮的tag值
        btn.tag = i;
        //这个属性不能关闭跳闪的动画
//        btn.adjustsImageWhenHighlighted = NO;
        //默认选中第0个按钮
        if (i == 0) {
            self.selectedButton = btn;
            self.selectedButton.selected = YES;
            
        }
        
        //监听按钮的点击事件
        [btn addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchDown];
        
        //将button添加到父视图上
        [tabbarView addSubview:btn];
    }
}

- (void)selectedBtn:(QTButton *)button {
    
    self.selectedButton.selected = NO;
    
    self.selectedButton = button;
    self.selectedButton.selected = YES;
    
    
    //切换控制器
    if (button.tag < 4) {
        self.selectedIndex = button.tag;
        self.moreImageView.hidden = YES;
    } else if (button.tag == 4) {
        self.moreImageView.hidden = NO;
    }
    switch (button.tag) {
            case MoreImageViewButtonTypeYao:
                NSLog(@"点击了摇一摇");
                break;
            case MoreImageViewButtonTypeContact:
                NSLog(@"点击了联系商家");
                break;
            case MoreImageViewButtonTypeZhibo:
                NSLog(@"点击了直播");
                break;
        case MoreImageViewButtonTypeWe:{
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            QTFeedbackViewController *feedbackVC = [storyBoard instantiateViewControllerWithIdentifier:@"QTFeedbackViewController"];
            [self presentViewController:feedbackVC animated:NO completion:nil];
        }
                break;
            case MoreImageViewButtonTypeBack:
                NSLog(@"点击了返回");
                break;
            default:
                break;
    }
    
    
    
}

- (void)addMoreView {
    
    //创建imageView
    UIImageView *moreImgView = [[UIImageView alloc] init];
    
    //开启用户交互
    moreImgView.userInteractionEnabled = YES;
    
    //属性记录
    self.moreImageView = moreImgView;
    
    //设置背景图片
    moreImgView.image = [UIImage imageNamed:@"home_topbar"];
    
    //添加到父视图上
    [self.view addSubview:moreImgView];
    
    //设置约束
    [moreImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.tabBar.mas_top);
        make.height.mas_equalTo(MoreImageViewHeight);
    }];
    
    //创建按钮
    
    UIButton *yaoBtn = [[UIButton alloc] init];
    //设置title
    [yaoBtn setTitle:@"摇一摇" forState:UIControlStateNormal];
    //设置字体颜色
    [yaoBtn setTintColor:[UIColor whiteColor]];
    
    //设置tag值
    yaoBtn.tag = MoreImageViewButtonTypeYao;
    
    //添加到父视图上
    [moreImgView addSubview:yaoBtn];
    
    //设置约束
    [yaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(moreImgView);
        make.left.equalTo(moreImgView).offset(25);
    }];
    
    UIButton *contactBtn = [[UIButton alloc] init];
    //设置title
    [contactBtn setTitle:@"联系商家" forState:UIControlStateNormal];
    //设置字体颜色
    [contactBtn setTintColor:[UIColor whiteColor]];
    
    //设置tag值
    contactBtn.tag = MoreImageViewButtonTypeContact;
    
    //添加到父视图上
    [moreImgView addSubview:contactBtn];
    
    //设置约束
    [contactBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(moreImgView);
        make.left.equalTo(yaoBtn.mas_right).offset(45);
    }];
    
    
    UIButton *zhiboBtn = [[UIButton alloc] init];
    //设置title
    [zhiboBtn setTitle:@"直播" forState:UIControlStateNormal];
    //设置字体颜色
    [zhiboBtn setTintColor:[UIColor whiteColor]];
    //设置tag值
    zhiboBtn.tag = MoreImageViewButtonTypeZhibo;
    
    //添加到父视图上
    [moreImgView addSubview:zhiboBtn];
    
    //设置约束
    [zhiboBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(moreImgView);
        make.left.equalTo(contactBtn.mas_right).offset(45);
    }];
    
    
    UIButton *weBtn = [[UIButton alloc] init];
    //设置title
    [weBtn setTitle:@"我们" forState:UIControlStateNormal];
    //设置字体颜色
    [weBtn setTintColor:[UIColor whiteColor]];
    
    //设置tag值
    weBtn.tag = MoreImageViewButtonTypeWe;
    
    //添加到父视图上
    [moreImgView addSubview:weBtn];
    
    //设置约束
    [weBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(moreImgView);
        make.left.equalTo(zhiboBtn.mas_right).offset(45);
    }];
    
    UIButton *backBtn = [[UIButton alloc] init];
    //设置title
    //[backBtn setTitle:@"" forState:UIControlStateNormal];
    //设置背景颜色
    //[backBtn setBackgroundColor:[UIColor blueColor]];
    
    //设置tag值
    backBtn.tag = MoreImageViewButtonTypeBack;
    
    //添加到父视图上
    [moreImgView addSubview:backBtn];
    
    //设置约束
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(moreImgView);
        make.left.equalTo(weBtn.mas_right).offset(10);
        make.height.width.mas_equalTo(44);
    }];
    
    //遍历moreImgView,获取子视图
    for (UIButton *btn in moreImgView.subviews) {
        [btn addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
